package com.geyue.controller;

import com.geyue.common.toolbox.result.Res;
import com.geyue.core.model.ProductProp;
import com.geyue.core.service.ProductPropService;
import com.geyue.core.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Lyon on 2017/5/16.
 */
@Controller
@RequestMapping("/prop")
public class PropController {

    @Autowired
    private ProductService productService;
    @Autowired
    private ProductPropService propService;

    @RequestMapping(value = "/{pid}", method = RequestMethod.GET)
    public String list(@PathVariable Integer pid, Model model) {

        ProductProp prop = new ProductProp();
        prop.setPid(pid);
        model.addAttribute("data", propService.getList(prop));
        model.addAttribute("pid", pid);
        return "prop/list";
    }

    @RequestMapping(value = "/add/{pid}", method = RequestMethod.GET)
    public String add_html(@PathVariable Integer pid, Model model) {
        model.addAttribute("pid", pid);
        return "prop/add";
    }

    @RequestMapping(value = "/", method = RequestMethod.POST)
    @ResponseBody
    public Res add(ProductProp prop) {

        if(propService.saveNotEmpty(prop) > 0)
            return Res.success("添加成功!");

        return Res.fail("添加失败!");
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public String edit_html(@PathVariable Integer id, Model model) {
        model.addAttribute("prop", propService.getOneByKey(id));
        return "prop/edit";
    }

    @RequestMapping(value = "/", method = RequestMethod.PUT)
    @ResponseBody
    public Res edit(ProductProp prop) {

        if(propService.updateNotEmpty(prop) > 0)
            return Res.success("修改成功!");

        return Res.fail("修改失败!");
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public Res del(@PathVariable Integer id) {

        if(propService.deleteByKey(id) > 0)
            return Res.success("删除成功!");

        return Res.fail("删除失败!");
    }

}
